import Vue from 'vue'
import Router from 'vue-router'
import Login from './views/Login.vue'

Vue.use(Router)

const originalPush = Router.prototype.push;

Router.prototype.push = function push(location) {

  return originalPush.call(this, location).catch(err => err)

}

const router = new Router({
  // mode: 'history',
  routes: [
    {
      path: '/zmkm',
      name: 'zmkm',
      component: Login
    },
    {
      path: '/register',
      name: 'register',
      component: () => import(/* webpackChunkName: "register" */ './views/Register.vue'),
      // meta: { keepAlive: true }
    },
    {
      path: '/privacy',
      name: 'privacy',
      component: () => import(/* webpackChunkName: "privacy" */ './views/Privacy.vue'),
      // meta: { keepAlive: true }
    },
    {
      path: '/forgot',
      name: 'forgot',
      component: () => import(/* webpackChunkName: "forgot" */ './views/Forgot.vue')
    },
    {
      path: '/',
      name: 'index',
      component: () => import(/* webpackChunkName: "index" */ './views/Index.vue')
    },
    {
      path: '/notice',
      name: 'notice',
      component: () => import(/* webpackChunkName: "notice" */ './views/Notice.vue')
    },
    {
      path: '/basicinfo',
      name: 'basicinfo',
      component: () => import(/* webpackChunkName: "basicinfo" */ './views/BasicInfo.vue'),
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/delayapply',
      name: 'delayApply',
      component: () => import(/* webpackChunkName: "delayapply" */ './views/DelayApply.vue'),
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/signup',
      name: 'signup',
      component: () => import(/* webpackChunkName: "signup" */ './views/Signup.vue'),
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/selectgrade',
      name: 'selectgrade',
      component: () => import(/* webpackChunkName: "selectgrade" */ './views/SelectGrade.vue'),
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/results',
      name: 'results',
      component: () => import(/* webpackChunkName: "results" */ './views/Results.vue'),
      meta: {
        requireAuth: true
      }
    },
    {
      path: "/empty",
      name: "empty",
      component: () =>
        import(/* webpackChunkName: "empty" */ "./views/Empty.vue")
    },
    {
      path: '/error',
      name: 'error',
      component: () =>
        import(/* webpackChunkName: "error" */ "./views/Error.vue")
    },
    {
      path: "*",
      redirect: '/error',
    }
  ]
})

// 路由拦截
router.beforeEach((to, from, next) => {
  if (to.matched.some(res => res.meta.requireAuth)) {// 判断是否需要登录权限
    //console.log(localStorage.getItem('TOKEN'))
    if (localStorage.getItem('TOKEN') || sessionStorage.getItem('TOKEN')) {// 判断是否登录
      next()
    } else {// 没登录则跳转到登录界面
      next({
        path: '/zmkm',
      })
    }
  } else {
    next()
  }
})

export default router